import cryptodes from "@/utils/crypto-js.min.js"
cryptodes.encryptByDES=function (message, key) {
	if(key==undefined){
		key='1, 9, 8, 2, 0, 8, 2, 1';
	}
    // For the key, when you pass a string,
    // it's treated as a passphrase and used to derive an actual key and IV.
    // Or you can pass a WordArray that represents the actual key.
    // If you pass the actual key, you must also pass the actual IV.
    var keyHex = cryptodes.enc.Utf8.parse(key);
    //console.log(cryptodes.enc.Utf8.stringify(keyHex), cryptodes.enc.Hex.stringify(keyHex));
    //console.log(cryptodes.enc.Hex.parse(cryptodes.enc.Utf8.parse(key).toString(cryptodes.enc.Hex)));
 
    // cryptodes use CBC as the default mode, and Pkcs7 as the default padding scheme
    var encrypted = cryptodes.DES.encrypt(message, keyHex, {
        mode: cryptodes.mode.ECB,
        padding: cryptodes.pad.Pkcs7
    });
    // decrypt encrypt result
    // var decrypted = cryptodes.DES.decrypt(encrypted, keyHex, {
    //     mode: cryptodes.mode.ECB,
    //     padding: cryptodes.pad.Pkcs7
    // });
    // console.log(decrypted.toString(cryptodes.enc.Utf8));
 
    // when mode is cryptodes.mode.CBC (default mode), you must set iv param
    // var iv = 'inputvec';
    // var ivHex = cryptodes.enc.Hex.parse(cryptodes.enc.Utf8.parse(iv).toString(cryptodes.enc.Hex));
    // var encrypted = cryptodes.DES.encrypt(message, keyHex, { iv: ivHex, mode: cryptodes.mode.CBC });
    // var decrypted = cryptodes.DES.decrypt(encrypted, keyHex, { iv: ivHex, mode: cryptodes.mode.CBC });
 
    // console.log('encrypted.toString()  -> base64(ciphertext)  :', encrypted.toString());
    // console.log('base64(ciphertext)    <- encrypted.toString():', encrypted.ciphertext.toString(cryptodes.enc.Base64));
    // console.log('ciphertext.toString() -> ciphertext hex      :', encrypted.ciphertext.toString());
    return encrypted.toString();
};
cryptodes.decryptByDES=function (ciphertext, key) {
	if(key==undefined){
		key='1, 9, 8, 2, 0, 8, 2, 1';
	}
    var keyHex = cryptodes.enc.Utf8.parse(key);
 
    // direct decrypt ciphertext
    var decrypted = cryptodes.DES.decrypt({
        ciphertext: cryptodes.enc.Base64.parse(ciphertext)
    }, keyHex, {
        mode: cryptodes.mode.ECB,
        padding: cryptodes.pad.Pkcs7
    });
 
    return decrypted.toString(cryptodes.enc.Utf8);
}
export default cryptodes